package dex.studio.compose.news.ui.pages

import android.provider.Browser
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import dex.studio.compose.news.ui.NewsTopAppBar
import dex.studio.compose.news.ui.data.bean.NewsBean
import dex.studio.compose.news.ui.tools.NewsNavigation
import android.widget.LinearLayout
import android.widget.ScrollView
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Text

import com.just.agentweb.AgentWeb


@Composable
fun NewsDetailPage(newsBean: NewsBean) {
    Scaffold(
        topBar = {
            NewsTopAppBar(
                title = "News Detail-${newsBean.title}",
                isShowBack = true,
                onClickBack = { NewsNavigation.backAndReturnsIsLastPage() }
            )
        },
        content = { NewsInfoWebView(newsBean) }
    )
}

@Composable
fun TestView(newsBean: NewsBean) {
    val currentContext = LocalContext.current
    AndroidView<TextView>(
        { TextView(currentContext) },
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .background(Color.Red)
    ) {
        it.text = "assss"
    }
}

@Composable
fun NewsInfoWebView(newsBean: NewsBean) {
    val current = LocalContext.current
    AndroidView(
        {
            LinearLayout(it)
        },
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .background(Color.Red)
    ) {

        AgentWeb.with(current as AppCompatActivity)
            .setAgentWebParent(it, LinearLayout.LayoutParams(-1, -1))
            .useDefaultIndicator()
            .createAgentWeb()
            .ready()
            .go(newsBean.path)
//        it.loadUrl("https://www.baidu.com")
//        it.webViewClient = object : WebViewClient() {
//            override fun shouldOverrideUrlLoading(
//                view: WebView?,
//                request: WebResourceRequest?
//            ): Boolean {
//                view?.loadUrl("https://www.baidu.com")
//                return true
//            }
//        }


    }
}